package dba.example.guice.dao

import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import org.scalatest.matchers.ShouldMatchers
import scala.collection.JavaConverters._
import dba.example.ExampleGuiceInjector
import dba.example.entities.Company

@RunWith(classOf[JUnitRunner])
class TestCompanyDao extends ExampleGuiceInjector with ShouldMatchers {
  
  val dao = injector.getInstance(classOf[CompanyDao])
  
  test("Test get Company") {
    val company = dao.get("COMPANY 1")
    
    company should not be(null)
    company.name should equal("Company 1")
  }
  
  test("Test fail to get Company") {
    val company = dao.get("Blah de blah blah")
    company should be(null)
  }
  
  test("Test create Company") {
    val company = Company("Company Name");
    val persisted = dao.save(company)
    company.name should equal(persisted.name)
    persisted.id should not be(null)
  }

  test("Test create Company twice") {
    val company = Company("Company Name");
    val persisted = dao.save(company)
    persisted.id should not be(null)

    val duplicateCompany = Company("Company Name");
    intercept[Exception] {
      dao.save(duplicateCompany)
    }
  }
}